package com.itheima.jobs;

import com.itheima.constant.RedisConstant;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Set;

/**
 * 自定义Job 实现定时清理垃圾图片
 */
public class ClearImgJob {
    @Autowired
    private JedisPool jedisPool;

    public void clearImg(){
        //根据redis里保存的俩个set的差值，获取垃圾图片的集合
        Set<String> set = jedisPool.getResource().sdiff(RedisConstant.SETMEAL_PIC_RESOURCES, RedisConstant.SETMEAL_PIC_DB_RESOURCES);

        if(set != null){
            for (String picName : set) {
                //调用七牛云工具类删除七牛云服务器的图片
                QiniuUtils.deleteFileFromQiniu(picName);

                Jedis resource = jedisPool.getResource();
                //删除redis中已从七牛云中清理的垃圾图片名称
                resource.srem(RedisConstant.SETMEAL_PIC_RESOURCES,picName);

                //归还redis
                resource.close();

            }
        }
    }
}
